package com.shunli.cterminal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.shunli.cterminal.model.dto.IpInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @ClassName: CommonServiceImpl
 * @Description: TODO
 * @Author: DAIYALU
 * @Date: 2024/4/11
 * @Version: 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CommonServiceImpl {

    private final IpService ipService;

    public String resolveIp(String ip) {
        if (StrUtil.isBlank(ip)) {
            return StringPool.EMPTY;
        }
        try {
            IpInfo ipInfo = ipService.getRegion(ip);
            if (StrUtil.isBlank(ipInfo.getCountry())) {
                return StringPool.EMPTY;
            }
            if (!"中国".equals(ipInfo.getCountry())) {
                return ipInfo.getCountry();
            }
            return ipInfo.getCountry().concat(ipInfo.getProvince()).concat(ipInfo.getCity()).concat("【")
                    .concat(ipInfo.getServiceProvider()).concat("】");
        } catch (Exception e) {
            log.warn("解析IP失败，IP：{}", ip);
        }
        return StringPool.EMPTY;
    }

}
